package workflow

import (
	hworkflow "zstackgo/pkg/header/core/workflow"
)

type FlowChainBuilder struct {
	FlowClassNames []string
	Flows          []hworkflow.Flow
	IsConstructed  bool
}

func (b *FlowChainBuilder) Construct() *FlowChainBuilder {
	if len(b.FlowClassNames) > 0 {
		for _, name := range b.FlowClassNames {
			flow := GetFlowInstance(name)
			b.Flows = append(b.Flows, flow)
		}
	}
	b.IsConstructed = true

	return b
}

func (b *FlowChainBuilder) SetFlowClassNames(flowClassNames []string) *FlowChainBuilder {
	b.FlowClassNames = flowClassNames
	return b
}

func (b *FlowChainBuilder) BuildName(name string) hworkflow.FlowChain {
	chain := b.Build()
	chain.SetName(name)
	return chain
}

func (b *FlowChainBuilder) Build() hworkflow.FlowChain {
	if !b.IsConstructed {
		panic("please call Construct() before Build()")
	}

	chain := NewSimpleFlowChain()
	for _, flow := range b.Flows {
		chain.Then(flow)
	}

	return chain
}

func NewBuilder() *FlowChainBuilder {
	return &FlowChainBuilder{}
}

func (b *FlowChainBuilder) GetFlows() []hworkflow.Flow {
	return b.Flows
}

func (b *FlowChainBuilder) SetFlows(flows []hworkflow.Flow) {
	b.Flows = flows
}

// todo
func GetFlowInstance(name string) hworkflow.Flow {
	return nil
}
